module Controllers.AddProblem(handleAddProblem) where

import Pages.AddProblem
import Controllers.Formlets

import State.Database
import State.TuuraQueries

import Happstack.State (update)
import Happstack.Server (toResponse,seeOther,ServerPart,Response)

-- | HTTP handler for the "Add a new problem" page
handleAddProblem :: String -- ^ The URL to return to after the form has been submitted
                     -> ServerPart Response -- ^ The Add problem form HTTP handler
handleAddProblem returnUrl =
    runSimpleForm "addProblem" problemForm (handleSubmit returnUrl)

-- | Handles the successful form submission
handleSubmit :: String -- ^ The URL to return to
             -> ProblemInfo -- ^ The submitted information
             -> ServerPart Response -- The HTTP response
handleSubmit returnUrl problem = do
    _problemId <- update (AddProblem problem)
    seeOther returnUrl (toResponse())
